package Day23.日期相关类;

import java.util.Calendar;
import java.util.Date;

/**
 * @author hyhWTX
 * @version 1.0
 * @date 2022年04月10日 12:08
 */


public class CalendarDemo2 {
    public static void main(String[] args) {
        Date date1 = new Date();
        //获得当前时间的Calendar对象
        Calendar calendar = Calendar.getInstance();
        //设置一个指定的日期
        calendar.setTime(date1);
        //获得90天以后的日期
        //calendar.add(Calendar.DATE,90);
        calendar.add(Calendar.DATE,90);
        String nowTime = getTimeStr(calendar);
        System.out.println(nowTime);
    }
    /**
     * 根据日历对象获得一个时间字符串
     * @param calendar
     */
    private static String getTimeStr(Calendar calendar) {
        //获得年
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR);
        int min = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String nowTime = year+"-"+fillZero(month)+"-"+fillZero(day)+" "+fillZero(hour)+":"+fillZero(min)+":"+fillZero(second);
        return nowTime;
    }

    private static String fillZero(int num){
        if(num<10){
            return "0"+num;
        }
        return num+"";
    }
}
